<?php

namespace Home\Controller;

/**
 * 用户
 * @auth zuly
 */
class User extends \App\Application {

	private $_userService;

	/**
	 * 构造函数
	 */
	public function __construct($appName, $controllerName, $actionName) {
		parent::__construct($appName, $controllerName, $actionName);
		$this->_userService = \Lsf\Loader::service('User', FALSE, APP_NAME_USER);
	}

	/**
	 * 注册
	 */
	public function reg() {
		$params = $this->post('', TRUE);
		if (!isset($params['user_name']) || empty($params['user_name'])) {
			return $this->errParamMissing(ECODE_PARAM_MISSING, 'user_name');
		}
		if (!isset($params['phone']) || empty($params['phone'])) {
			return $this->errParamMissing(ECODE_PARAM_MISSING, 'phone');
		}
		if (!isset($params['company_name']) || empty($params['company_name'])) {
			return $this->errParamMissing(ECODE_PARAM_MISSING, 'company_name');
		}
		if (!isset($params['employee_num']) || empty($params['employee_num'])) {
			return $this->errParamMissing(ECODE_PARAM_MISSING, 'employee_num');
		}
		if (!isset($params['open_id']) || empty($params['open_id'])) {
			return $this->errParamMissing(ECODE_PARAM_MISSING, 'open_id');
		}
		$result = $this->_userService->addUser($params['open_id'], $params['user_name'],$params['phone'],$params['company_name'],$params['employee_num']);
		if(!is_array($result) || empty($result)){
			return $this->json(4,[],'用户注册失败');
		}
		return $this->json(ECODE_SUCCESS, $result);
	}

	/**
	 * 修改个人信息
	 */
	public function edit() {
		$params = $this->post('', TRUE);
		if (!isset($params['user_id']) || empty($params['user_id'])) {
			return $this->errParamMissing(ECODE_PARAM_MISSING, 'user_id');
		}
		if (!isset($params['user_name']) || empty($params['user_name'])) {
			$params['user_name'] = '';
		}
		if (!isset($params['phone']) || empty($params['phone'])) {
			$params['phone'] = '';

		}
		if (!isset($params['company_name']) || empty($params['company_name'])) {
			$params['company_name'] = '';

		}
		if (!isset($params['employee_num']) || empty($params['employee_num'])) {
			$params['employee_num'] = '';
		}
		// 未修改则直接返回成功
		if(empty($params['user_name']) && empty($params['phone']) && empty($params['company_name']) && empty($params['employee_num'])){
			return $this->json(ECODE_SUCCESS, ['uid' => $params['user_id']]);
		}
		// 修改信息
		$result = $this->_userService->editUser($params['user_id'],$params['user_name'],$params['phone'],$params['company_name'],$params['employee_num']);
		if(!is_array($result) || empty($result)){
			return $this->json(4,[] ,'用户修改信息失败');
		}
		return $this->json(ECODE_SUCCESS, $result);
	}

	/**
	 * 获取个人信息
	 */
	public function info() {
		$params = $this->post('', TRUE);
		$result = $this->_userService->getUser($params['user_id'], $params['phone']);
		if(!is_array($result) || empty($result)){
			if($result == -1){
				return $this->json(5,[], '未处查询到用户');
			}
			return $this->json(4,[], '获取用户信息失败');
		}
		return $this->json(ECODE_SUCCESS, $result);
	}

	/**
	 * 获取明信片
	 */
	public function postcard() {
		$params = $this->post('', TRUE);
		if (!isset($params['user_id']) || empty($params['user_id'])) {
			return $this->errParamMissing(ECODE_PARAM_MISSING, 'user_id');
		}
		if (!isset($params['open_id']) || empty($params['open_id'])) {
			return $this->errParamMissing(ECODE_PARAM_MISSING, 'open_id');
		}
		$result = $this->_userService->getUser($params['user_id'],'');
		if(!is_array($result) || empty($result)){
			return $this->json(4,[],'获取用户明信片失败');
		}
		if(empty($result['card_url'])){
			return $this->json(ECODE_SUCCESS, ['url' => '']);
		}else {
			return $this->json(ECODE_SUCCESS, ['url' => $result['card_url']]);
		}
	}

	/**
	 * 提交明信片地址
	 */
	public function postcardSubmit() {
		$params = $this->post('', TRUE);
		if (!isset($params['uid']) || empty($params['uid'])) {
			return $this->errParamMissing(ECODE_PARAM_MISSING, 'uid');
		}
		if (!isset($params['url']) || empty($params['url'])) {
			return $this->errParamMissing(ECODE_PARAM_MISSING, 'url');
		}
		$result = $this->_userService->addCardUrl($params['uid'],$params['url']);
		if(!is_array($result) || empty($result)){
			return $this->json(4,[],'提交明信片地址');
		}
		return $this->json(ECODE_SUCCESS, []);
	}

	/**
	 * 总分
	 */
	public function totalPoint() {
		$params = $this->post('', TRUE);
		if (!isset($params['uid']) || empty($params['uid'])) {
			return $this->errParamMissing(ECODE_PARAM_MISSING, 'uid');
		}
		$result = $this->_userService->getUser($params['uid'],'');
		if(!is_array($result) || empty($result)){
			return $this->json(4,[],'获取用户总分失败');
		}
		return $this->json(ECODE_SUCCESS, [
			'uid' => $result['user_id'],
			'total_point' => $result['point'],
		]);
	}

	/**
	 * 微信登录
	 */
	public function openId() {
		$params = $this->post('', TRUE);
		if (!isset($params['uc']) || empty($params['uc'])) {
			return $this->errParamMissing(ECODE_PARAM_MISSING, 'uc');
		}
		$result = $this->_userService->getWxUserInfo($params['uc'],'');
		if(!is_array($result) || empty($result)){
			return $this->json(4,[],'获取用户信息失败');
		}
		return $this->json(ECODE_SUCCESS, $result);
	}

	/**
	 * 获取用户列表
	 */
	public function list() {
		$userName = $this->get('user_name', TRUE);
		$phone = $this->get('phone', TRUE);
		$pageSize = $this->get('page_size', TRUE);
		$pageNo = $this->get('page_no', TRUE);
		$result = $this->_userService->getUserList($userName,$phone,$pageSize,$pageNo);
		if(!is_array($result) || empty($result)){
			return $this->json(4,[],'获取用户列表失败');
		}
		return $this->json(ECODE_SUCCESS, $result);
	}

	/**
	 * 排行
	 */
	public function rankList() {
		$result = $this->_userService->getRankList();
		if(!is_array($result) || empty($result)){
			return $this->json(4,[],'获取用户列表失败');
		}
		return $this->json(ECODE_SUCCESS, $result);
	}
}
